package androidApplication;

import java.util.ArrayList;

import security.Encryption;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;

import capstone.aw_capstone_androidapplication.R;
import entities.Device;


public class MainOverview extends Activity{
	private TextView lblDeviceName;
	private TextView lblDeviceIP;
	private TextView lblDeviceUUID;
	
	private Button btnDeviceList;
	private Button btnDeviceMap;
	private Button btnPairing;
	private Button btnAddDevice;
	private Button btnRemoveDevice;
	
	private Commons common;
	
	public MainOverview(){
		
	}
	
	public void onCreate(Bundle b){
		super.onCreate(b);
		setContentView(R.layout.mainoverview);
		this.common = (Commons) getIntent().getSerializableExtra("Commons");
				
		btnDeviceList = (Button) findViewById(R.id.btnDeviceList);
		btnDeviceList.setOnClickListener(btnDeviceListClicked);
		
		btnDeviceMap = (Button) findViewById(R.id.btnDeviceMap);
		btnDeviceMap.setOnClickListener(btnDeviceMapClicked);
		
		btnPairing = (Button) findViewById(R.id.btnPairing);
		btnPairing.setOnClickListener(btnPairingClicked);
		
		btnAddDevice = (Button) findViewById(R.id.btnAddDevice);
		btnAddDevice.setOnClickListener(btnAddDeviceClicked);
		
		btnRemoveDevice = (Button) findViewById(R.id.btnRemoveDevice);
		btnRemoveDevice.setOnClickListener(btnRemoveDeviceClicked);

		lblDeviceName = (TextView) findViewById(R.id.lblDeviceName);
		Log.i("MainOverview", common.getThisDevice().toString());
		Log.i("MainOverview", common.getThisDevice().getDeviceName());
		lblDeviceName.setText(common.getThisDevice().getDeviceName());
		
		lblDeviceIP = (TextView) findViewById(R.id.lblDeviceIP);
		lblDeviceIP.setText(common.getThisDevice().getIpAddress());
		
		lblDeviceUUID = (TextView) findViewById(R.id.lblDeviceUUID);
		lblDeviceUUID.setText(common.getThisDevice().getUUID());
	}

	private final Button.OnClickListener btnDeviceListClicked = new Button.OnClickListener(){
		@Override
		public void onClick(View arg0) {
			Intent intent = new Intent(MainOverview.this.getApplication(), DeviceList.class);
			System.out.println(common.getThisDevice() + "DERPLE");
			for(String s : common.getThisDevice().getConnectedDevices()){
				System.out.println(s + "MAINOVERVIEWSLOOP");
			}
			intent.putExtra("Commons", common);
			startActivity(intent);
		}		
	};	
	private final Button.OnClickListener btnDeviceMapClicked = new Button.OnClickListener(){
		@Override
		public void onClick(View arg0){
			Intent intent = new Intent (MainOverview.this.getApplication(), DeviceMap.class);
			intent.putExtra("Commons", common);
			startActivity(intent);
		}
	};
	private final Button.OnClickListener btnPairingClicked = new Button.OnClickListener(){
		@Override
		public void onClick(View arg0){
			Intent intent = new Intent (MainOverview.this.getApplication(), Pairing.class);
			intent.putExtra("Commons", common);
			startActivity(intent);
		}
	};
	private final Button.OnClickListener btnAddDeviceClicked = new Button.OnClickListener(){
		@Override
		public void onClick(View arg0){
			EditText txtTargetIp = (EditText) findViewById(R.id.txtIP);
			String ip = txtTargetIp.getText().toString();
			EditText txtPairKey = (EditText) findViewById(R.id.txtPairKey);
			String pairKey = txtPairKey.getText().toString();
			String encPairKey = Encryption.digest(pairKey);
			common.getCsClient().connectDevices(common.getThisDevice().getUUID(), ip, encPairKey);
			Device tempD = common.getCsClient().getDevice(common.getThisDevice().getUUID(), false);
			common.setThisDevice(tempD);
		}
	};//
	private final Button.OnClickListener btnRemoveDeviceClicked = new Button.OnClickListener(){
		@Override
		public void onClick(View arg0){
			
		}
	};
}
